package com.losdun.weixin.controller.app;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.losdun.common.annotation.Anonymous;
import com.losdun.common.config.LosdunConfig;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.domain.model.LoginUser;
import com.losdun.common.utils.file.FileUploadUtils;
import com.losdun.common.utils.file.MimeTypeUtils;
import com.losdun.weixin.common.WxConstants;
import com.losdun.weixin.domain.ShareImage;
import com.losdun.weixin.domain.WechatUser;
import com.losdun.weixin.security.WxSecurityUtils;
import com.losdun.weixin.service.IShareImageService;
import com.losdun.weixin.service.WechatUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author sword.ye
 * @date 2025/6/8
 **/
@Slf4j
@RestController
@RequestMapping("/app/wx")
public class AppWeixinController extends BaseController {

    @Autowired
    private WxMaService wxMaService;
    @Autowired
    private IShareImageService shareImageService;
    @Autowired
    private WechatUserService wechatUserService;

    @Anonymous
    @GetMapping(value = "/miniapp/qrcode")
    public void getQrCode(@RequestParam String scene,@RequestParam(defaultValue = "pages/index/index") String page, HttpServletResponse response) throws IOException, WxErrorException {
        byte[] qrCode = wxMaService.getQrcodeService().createWxaCodeUnlimitBytes(scene,page,false,"release",400,true,null,false);
        // 返回图片流
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        response.getOutputStream().write(qrCode);
    }

    @Anonymous
    @GetMapping(value = "/shareImage/list")
    public AjaxResult listShareImage() {
        List<ShareImage> list = shareImageService.selectShareImageList(new ShareImage());
        return success(list);
    }


    /**
     * 获取订阅消息
     */
    @Anonymous
    @GetMapping(value = "/subscribeMsgId")
    public AjaxResult getSubscribeMsgId()
    {
        return success(WxConstants.getAllSubscribeMsgId());
    }

    @GetMapping("/user/detail")
    public AjaxResult getWxUserInfo()
    {
        WechatUser user = wechatUserService.getById(WxSecurityUtils.getUserId());
        return success(user);
    }

    @PostMapping("/user/update")
    public AjaxResult edit(@RequestBody WechatUser wechatUser){
        wechatUser.setId(WxSecurityUtils.getUserId());
        wechatUserService.updateById(wechatUser);
        return success();
    }



}
